// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        tempPrefab:{
            default: null,
            type: cc.Node
        },

        maxCount: 0,

        cellWidth: 100,

        cellHeight: 100,

        childList:{
            default: [],
            type: Array
        },
    },

    // LIFE-CYCLE CALLBACKS:

    setMaxCount(count, icon){
        this.maxCount = count;
        this.icon = icon;
        this.updateCell()
    },

    updateCell(){
        let tempLength = Math.max(this.maxCount, this.childList.length);
        let tempNum1 = Math.floor(this.maxCount / 2);
        let tempNum2 = this.maxCount % 2;
        for (var i = 0; i < tempLength; i++) {

            if (i < this.maxCount) {
                if (i > this.childList.length - 1) {
                    let temp = cc.instantiate(this.tempPrefab);
                    this.node.addChild(temp);
                    this.childList.push(temp);
                }
                this.childList[i].active = true;
                // this.childList[i].getComponent('Sprite').spriteFrame = this.icon;
                let posX = this.cellWidth * (i - tempNum1);
                if(tempNum2 == 0)
                {
                    posX += this.cellWidth / 2;
                }
                this.childList[i].setPosition(posX, 0);
            }else{
                if (i < this.childList.length) {
                    this.childList[i].active = false;
                }
            }
        }
        this.tempPrefab.active = false;
    },

    // =onLoad () {},

    start () {

    },

    // update (dt) {},
});
